<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模板表ID" prop="templateId">
        <el-input
          v-model="queryParams.templateId"
          placeholder="请输入模板表ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模板编码" prop="templateNumber">
        <el-input
          v-model="queryParams.templateNumber"
          placeholder="请输入模板编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模板名称" prop="templateName">
        <el-input
          v-model="queryParams.templateName"
          placeholder="请输入模板名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模板状态" prop="templateStatus">
        <el-select v-model="queryParams.templateStatus" placeholder="请选择模板状态" clearable>
          <el-option
            v-for="dict in dict.type.yh_template_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="数据库表名" prop="databankName">
        <el-input
          v-model="queryParams.databankName"
          placeholder="请输入数据库表名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据库表编码" prop="databankNumber">
        <el-input
          v-model="queryParams.databankNumber"
          placeholder="请输入数据库表编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字段名称" prop="fieldName">
        <el-input
          v-model="queryParams.fieldName"
          placeholder="请输入字段名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字段编码" prop="fieldNumber">
        <el-input
          v-model="queryParams.fieldNumber"
          placeholder="请输入字段编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字段类型" prop="fieldKind">
        <el-input
          v-model="queryParams.fieldKind"
          placeholder="请输入字段类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字段长度" prop="fieldLength">
        <el-input
          v-model="queryParams.fieldLength"
          placeholder="请输入字段长度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字段顺序" prop="fieldSeq">
        <el-input
          v-model="queryParams.fieldSeq"
          placeholder="请输入字段顺序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="默认导出状态" prop="fieldDeriveStatus">
        <el-select v-model="queryParams.fieldDeriveStatus" placeholder="请选择默认导出状态" clearable>
          <el-option
            v-for="dict in dict.type.yh_field_derive_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="字段开关状态" prop="fieldSwitchStatus">
        <el-select v-model="queryParams.fieldSwitchStatus" placeholder="请选择字段开关状态" clearable>
          <el-option
            v-for="dict in dict.type.yh_field_switch_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:dcfield:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:dcfield:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:dcfield:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['project:dcfield:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="EXAdd"
        >自定义导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dcfieldList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="模板表ID" align="center" prop="templateId" />
      <el-table-column label="模板编码" align="center" prop="templateNumber" />
      <el-table-column label="模板名称" align="center" prop="templateName" />
      <el-table-column label="模板状态" align="center" prop="templateStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yh_template_status" :value="scope.row.templateStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="数据库表名" align="center" prop="databankName" />
      <el-table-column label="数据库表编码" align="center" prop="databankNumber" />
      <el-table-column label="字段名称" align="center" prop="fieldName" />
      <el-table-column label="字段编码" align="center" prop="fieldNumber" />
      <el-table-column label="字段类型" align="center" prop="fieldKind" />
      <el-table-column label="字段长度" align="center" prop="fieldLength" />
      <el-table-column label="字段顺序" align="center" prop="fieldSeq" />
      <el-table-column label="默认导出状态" align="center" prop="fieldDeriveStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yh_field_derive_status" :value="scope.row.fieldDeriveStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="字段开关状态" align="center" prop="fieldSwitchStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yh_field_switch_status" :value="scope.row.fieldSwitchStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['project:dcfield:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:dcfield:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改模板字段对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="模板表ID" prop="templateId">
          <el-input v-model="form.templateId" placeholder="请输入模板表ID" />
        </el-form-item>
        <el-form-item label="模板编码" prop="templateNumber">
          <el-input v-model="form.templateNumber" placeholder="请输入模板编码" />
        </el-form-item>
        <el-form-item label="模板名称" prop="templateName">
          <el-input v-model="form.templateName" placeholder="请输入模板名称" />
        </el-form-item>
        <el-form-item label="模板状态" prop="templateStatus">
          <el-radio-group v-model="form.templateStatus">
            <el-radio
              v-for="dict in dict.type.yh_template_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="数据库表名" prop="databankName">
          <el-input v-model="form.databankName" placeholder="请输入数据库表名" />
        </el-form-item>
        <el-form-item label="数据库表编码" prop="databankNumber">
          <el-input v-model="form.databankNumber" placeholder="请输入数据库表编码" />
        </el-form-item>
        <el-form-item label="字段名称" prop="fieldName">
          <el-input v-model="form.fieldName" placeholder="请输入字段名称" />
        </el-form-item>
        <el-form-item label="字段编码" prop="fieldNumber">
          <el-input v-model="form.fieldNumber" placeholder="请输入字段编码" />
        </el-form-item>
        <el-form-item label="字段类型" prop="fieldKind">
          <el-input v-model="form.fieldKind" placeholder="请输入字段类型" />
        </el-form-item>
        <el-form-item label="字段长度" prop="fieldLength">
          <el-input v-model="form.fieldLength" placeholder="请输入字段长度" />
        </el-form-item>
        <el-form-item label="字段顺序" prop="fieldSeq">
          <el-input v-model="form.fieldSeq" placeholder="请输入字段顺序" />
        </el-form-item>
        <el-form-item label="默认导出状态" prop="fieldDeriveStatus">
          <el-radio-group v-model="form.fieldDeriveStatus">
            <el-radio
              v-for="dict in dict.type.yh_field_derive_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="字段开关状态" prop="fieldSwitchStatus">
          <el-radio-group v-model="form.fieldSwitchStatus">
            <el-radio
              v-for="dict in dict.type.yh_field_switch_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 弹出的字段弹出层 -->
    <el-dialog :title="title" :visible.sync="ziduanopen" width="80%" append-to-body>
      <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange" style="margin-left: 0.7%">字段全选</el-checkbox>
      <el-checkbox-group v-model="checkList">
        <el-checkbox v-for="(city,index) in ziduanList" :label="city.nameEn" :key="index" class="checkbox-item">{{ city.nameCn }}</el-checkbox>
      </el-checkbox-group>
      <div slot="footer" class="dialog-footer">
         <el-button type="primary" @click="daochu">导 出</el-button>
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import { listDcfield, getDcfield, delDcfield, addDcfield, updateDcfield,zidingyiexport,getfields} from "@/api/module/ny/dcfield";
  import { Message } from 'element-ui';
  export default {
    name: "Dcfield",
    dicts: ['yh_field_switch_status', 'yh_template_status', 'yh_field_derive_status'],
    data() {
      return {
        showCheck: false,

        isIndeterminate: false,

        checkList:[],

        checkAll: false,
        //回显列表
        nameList: [],
        //选择列表
        checkItem: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 模板字段表格数据
        dcfieldList: [],
        //返回的字段列表
        ziduanList:[],

        // 是否显示弹出层
        ziduanopen: false,
        //需要查询的表信息
        table:{
          // tableName:"hu_emergency_records",
          tableName:"",
          tableNumber:"module",
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          templateId: null,
          templateNumber: null,
          templateName: null,
          templateStatus: null,
          databankName: null,
          databankNumber: null,
          fieldName: null,
          fieldNumber: null,
          fieldKind: null,
          fieldLength: null,
          fieldSeq: null,
          fieldDeriveStatus: null,
          fieldSwitchStatus: null,
        },
        export:{
          // dataTable:'hu_emergency_records',
          // dataTable:'hu_emergency_cases',
          dataTable:'',
          param:[],
          title:""
        },
        // 查询参数
        Params: {
          template_number: null,
          template_name: null,
          template_status: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
        // 表单参数
        daochuform: {},
      };
    },
    created() {
      this.getList();
      // this.getEXPORTList()
    },
    methods: {
      handleCheckedCitiesChange(value) {
        let checkedCount = value.length;
        this.checkAll = checkedCount === this.nameList.length;
        this.isIndeterminate = checkedCount > 0 && checkedCount < this.nameList.length;
      },
      /** 查询模板字段列表 */
      getList() {
        this.loading = true;
        listDcfield(this.queryParams).then(response => {
          this.dcfieldList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      getEXPORTList() {
        // this.loading = true;
        getfields(this.table).then(response => {
          // alert(response)
          // this.ziduanListChina = response[0]
          this.ziduanList = response
          console.log(this.ziduanList)
        });
      },
      // 全选按钮状态改变时的处理函数
      handleCheckAllChange(val) {
        this.checkList = val ? this.ziduanList.map(city => city.nameEn) : [];
        this.isIndeterminate = false; // 设置为false，表示不再是半选状态
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.ziduanopen = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          templateId: null,
          templateNumber: null,
          templateName: null,
          templateStatus: null,
          databankName: null,
          databankNumber: null,
          fieldName: null,
          fieldNumber: null,
          fieldKind: null,
          fieldLength: null,
          fieldSeq: null,
          fieldDeriveStatus: null,
          fieldSwitchStatus: null,
        };
        this.resetForm("form");
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加模板字段";
      },
      /** 自定义导出按钮操作 */
      EXAdd() {
        this.getEXPORTList();
        this.ziduanopen = true;
        this.title = "导出自定义字段";
      },
      //导出方法
      daochu(){
        this.export.param=this.checkList
        zidingyiexport(this.export).then(response => {
          if (response == 200) {
            Message.success('导出成功，请在桌面查看');
            this.ziduanopen = false;
          } else if (response == 500) {
            Message.error('请选择导出字段');
          }else {
            Message.error('导出失败，请稍后重试');
          }
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getDcfield(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改模板字段";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateDcfield(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addDcfield(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除模板字段编号为"' + ids + '"的数据项？').then(function() {
          return delDcfield(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('project/dcfield/export', {
          ...this.queryParams
        }, `dcfield_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
<style lang="scss" scoped>
  .checkbox-group {
    display: flex;
    flex-wrap: wrap;
  }

  .checkbox-item {
    width: 10%; /* 一行5个，每个宽度为20% */
    box-sizing: border-box; /* 让padding和border计入宽度 */
    padding: 10px; /* 设置内边距 */
    box-sizing: border-box; /* 让padding和border计入宽度 */
  }
</style>
